package com.itheima.web.controller.cargo;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.cargo.ContractProduct;
import com.itheima.domain.cargo.ContractProductExample;
import com.itheima.domain.cargo.Factory;
import com.itheima.domain.cargo.FactoryExample;
import com.itheima.service.cargo.ContractProductService;
import com.itheima.service.cargo.FactoryService;
import com.itheima.util.UploadUtil;
import com.itheima.web.controller.BaseController;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping(value = "/cargo/contractProduct")
public class ContractProductController extends BaseController {

    @Reference
    private ContractProductService contractProductService;

    @Reference
    private FactoryService factoryService;

    @RequestMapping(value = "/list", name = "分页查询货物列表")
    public String list(@RequestParam(defaultValue = "1") int page,
                       @RequestParam(defaultValue = "5") int size,
                       String contractId) {
        //1.查询所有货物信息
        ContractProductExample example = new ContractProductExample();
        example.createCriteria().andCompanyIdEqualTo(companyId).andContractIdEqualTo(contractId);
        PageInfo pageInfo = contractProductService.findAll(example, page, size);

        //2.存入request域中
        request.setAttribute("page", pageInfo);

        //3.通过factoryService查询所有货物的厂家
        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria().andCtypeEqualTo("货物");
        List<Factory> factoryList;
        factoryList = factoryService.findAll(factoryExample);

        //4.将货物的厂家放入request域
        request.setAttribute("factoryList", factoryList);
        //5.将contractId放入request域
        request.setAttribute("contractId", contractId);

        //跳转货物列表页面
        return "cargo/product/product-list";
    }

    @RequestMapping(value = "/edit", name = "货物更新或保存操作")
    public String edit(ContractProduct contractProduct, MultipartFile productPhoto) throws Exception {
        contractProduct.setCompanyId(companyId);
        contractProduct.setCompanyName(companyName);

        if (productPhoto != null) {
            String upload = new UploadUtil().upload(productPhoto.getBytes());
            contractProduct.setProductImage(upload);
        }

        if (StringUtils.isEmpty(contractProduct.getId())) {
            contractProduct.setId(UUID.randomUUID().toString());
            contractProductService.save(contractProduct);
        } else {
            contractProductService.update(contractProduct);
        }
        return "redirect:/cargo/contractProduct/list.do?contractId=" + contractProduct.getContractId();
    }

    @RequestMapping(value = "/toUpdate", name = "跳转到货物编辑页面")
    public String toUpdate(String id) {
        FactoryExample example = new FactoryExample();
        example.createCriteria().andCtypeEqualTo("货物");
        List<Factory> factoryList = factoryService.findAll(example);
        request.setAttribute("factoryList", factoryList);

        ContractProduct contractProduct = contractProductService.findById(id);
        request.setAttribute("contractProduct", contractProduct);
        return "cargo/product/product-update";
    }

    @RequestMapping(value = "/delete", name = "删除货物")
    public String delete(String id, String contractId) {
        contractProductService.delete(id);
        return "redirect:/cargo/contractProduct/list.do?contractId=" + contractId;
    }

    @RequestMapping(value = "/toImport", name = "跳转上传货物页面")
    public String toImport(String contractId) {
        request.setAttribute("contractId", contractId);
        return "cargo/product/product-import";
    }

    @RequestMapping(value = "/import", name = "上传货物")
    public String importExcel(MultipartFile file, String contractId) throws IOException {
        //1、读取excel表，创建工作薄，前端传入的file得到
        Workbook workbook = new XSSFWorkbook(file.getInputStream());
        //2、通过工作薄读取页，索引是0，实际上是第一页
        Sheet sheet = workbook.getSheetAt(0);
        //3、循环到最后一行，getLastRow有数据的最后一行的索引
        for (int i = 1; i <= sheet.getLastRowNum(); i++) {
            //4、在循环里读取每一行
            Row row = sheet.getRow(i);
            ContractProduct contractProduct = new ContractProduct();
            //5、读取每个单元格，写入contractProduct实体类，读取9个有值的单元格
            contractProduct.setFactoryName(row.getCell(1).getStringCellValue());
            contractProduct.setProductNo(row.getCell(2).getStringCellValue());
            contractProduct.setCnumber((int) row.getCell(3).getNumericCellValue());
            contractProduct.setPackingUnit(row.getCell(4).getStringCellValue());
            contractProduct.setLoadingRate(row.getCell(5).getNumericCellValue() + "");
            contractProduct.setBoxNum((int) row.getCell(6).getNumericCellValue());
            contractProduct.setPrice(row.getCell(7).getNumericCellValue());
            contractProduct.setProductDesc(row.getCell(8).getStringCellValue());
            contractProduct.setProductRequest(row.getCell(9).getStringCellValue());

            //设置实体类的货物的id，企业id，企业名称，合同id
            contractProduct.setContractId(contractId);
            contractProduct.setId(UUID.randomUUID().toString());
            contractProduct.setCompanyId(companyId);
            contractProduct.setCompanyName(companyName);

            //6、在循环里，调用contractProductService.save方法写入数据库
            contractProductService.save(contractProduct);
        }
        //7、跳转页面
        return "redirect:/cargo/contractProduct/list.do?contractId=" + contractId;
    }
}
